package com.vgemv.jsutilitysdk.preview;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.vgemv.jsutilitysdk.R;
import com.vgemv.jsutilitysdk.log.JsLogManager;

import java.io.IOException;

public class JsVideoPreviewActivity extends Activity {
    private MediaPlayer mPlayer = new MediaPlayer();
    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;
    private String mFilePath;
    private SurfaceHolder.Callback mSurfaceHolderCallback;
    public static String LOCAL_VIDEO_FILE_KEY="LocalVideoFile";
    private RelativeLayout mRl_root;
    private MediaPlayer.OnInfoListener mInfoListener;
    private MediaPlayer.OnPreparedListener mPreparedListener;
    private MediaPlayer.OnErrorListener mErrorListener;
    private MediaPlayer.OnCompletionListener mCompletionListener;

    private static String TAG = "JsVideoPreview";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RelativeLayout rl = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_js_video_preview,null);
        setContentView(rl);
        mRl_root = rl;

        mFilePath = getIntent().getStringExtra(LOCAL_VIDEO_FILE_KEY);
        if(mFilePath == null) {
            return;
        }
//        Uri uri = null;
        try {
            mPlayer.setDataSource(mFilePath);
            mPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mInfoListener = new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                //805 -22.
                switch (what) {
                    case MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:{
                        JsLogManager.logfc_v(TAG,"rendering start");
                        break;
                    }
                    //视频没有播放,音频正常播放.
                    case MediaPlayer.MEDIA_INFO_VIDEO_NOT_PLAYING:{
                        JsLogManager.logfc_v(TAG,"video not playing");
                        break;
                    }
                }
                return false;
            }
        };
        mErrorListener = new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                JsLogManager.logfc_e(TAG,"video not playing");
                return false;
            }
        };

        mPlayer.setOnInfoListener(mInfoListener);
        initPreviewView();
    }

    public void initPreviewView() {
        mSurfaceView = mRl_root.findViewById(R.id.previewView2);

        mSurfaceHolder = mSurfaceView.getHolder();

        mSurfaceHolderCallback = new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder) {

            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
                JsLogManager.logfc_i(TAG, "surface changed\n");
                mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                //设置显示视频显示在SurfaceView上
                mPlayer.setDisplay(holder);
                if(mFilePath != null) {
                    mPlayer.start();
                }
            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
                if(mPlayer.isPlaying()) {
                    mPlayer.stop();
                }
                mPlayer.release();
            }
        };
        mSurfaceHolder.addCallback(mSurfaceHolderCallback);
    }
}
